import com.estore.network.EStoreClient;
import com.estore.model.Product;
import com.estore.service.ProductService;
import java.util.HashMap;
import java.util.Map;

public class TestOrderCreation {
    public static void main(String[] args) {
        try {
            // 初始化商品服务并添加一些测试商品
            ProductService productService = new ProductService();
            
            // 确保有测试商品
            if (productService.findProductById("p001") == null) {
                System.out.println("商品数据未正确加载");
                return;
            }
            
            System.out.println("=== 测试订单创建 ===");
            
            // 模拟创建订单的商品映射
            Map<String, Integer> orderItems = new HashMap<>();
            orderItems.put("p001", 1); // 购买1本Java从入门到精通
            
            // 创建客户端实例
            EStoreClient client = new EStoreClient();
            
            // 使用反射调用私有方法进行测试
            java.lang.reflect.Method method = EStoreClient.class.getDeclaredMethod("createOrderViaNetwork", String.class, Map.class);
            method.setAccessible(true);
            
            try {
                // 测试订单创建
                Object result = method.invoke(client, "testUser", orderItems);
                if (result instanceof com.estore.model.Order) {
                    com.estore.model.Order order = (com.estore.model.Order) result;
                    System.out.println("✅ 订单创建成功!");
                    System.out.println("订单信息: " + order.toString());
                } else {
                    System.out.println("❌ 订单创建失败，返回结果类型错误");
                }
            } catch (Exception e) {
                System.out.println("❌ 订单创建失败: " + e.getCause().getMessage());
            }
            
        } catch (Exception e) {
            System.err.println("测试过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}